<?php

// Acceso restringido
defined('JL') or die('Acceso restringido');

define("MY_LOGOS", JLPATH_WEB_SITE . DS . "images" . DS . "logos");
define("MY_THUMBS", MY_LOGOS . DS . "thumbs");

define("MY_ANNONCES", JLPATH_WEB_SITE . DS . "images" . DS . "annonces");
define("MY_ANNONCES_THUMBS", MY_ANNONCES . DS . "thumbs");

define("MSG_SUCCESS", "Données sauvegarder avec succès.");
define("MSG_ERROR", "Les données ont été effacer avec succès.");
define("MSG_DELETE_SUCCESS", "Logo éliminé avec succès.");
define("MAX_HEIGHT_LOGO", 55);
define("MAX_WIDTH_ANNONCE", 450);

class LogosController extends JLControllerBase {

    private $config = null;
    private $path = "";
    private $title = "";
    private $data = null;

    public function crop() {
        if ($this->isSetNoEmpty($_POST['time'])) {
            $x1 = $_POST['x1'];
            $y1 = $_POST['y1'];
            $width = $_POST['width'];
            $height = $_POST['height'];
            $name = $_POST['name'];
            $annonce = $_POST['annonce'];

            $id = ($this->isSetNoEmpty($_POST['id'])) ? $_POST['id'] : null;
            $beforename = null;

            if ($id) {
                require JLPATH_MODELS . DS . "LogosModel.php";
                $logos = new LogosModel();

                $image = $logos->getLogo($id);
                $beforename = $image[0]['var_src'];
            }

            $this->makeCropImage($x1, $y1, $width, $height, $name, $beforename, $annonce);
        }
    }

    private function makeCropImage($x1, $y1, $width, $height, $name, $beforename = null, $annonce = FALSE) {
        $jl = new JLTools();
        $newname = ($beforename) ? $beforename : "logo-" . strtolower($jl->RandomNumerosLetras(10, "ambos") . ".jpg");
        $newpath = ($annonce ? MY_ANNONCES : MY_LOGOS) . DS . $newname;
        $oldpath = ($annonce ? MY_ANNONCES : MY_LOGOS) . DS . $name;

        require JLPATH_LIBRARIES . DS . 'Tools' . DS . 'JLImage.class.php';

        $image = new JLImage();
        $image->load($oldpath);
        $image->crop($width, $height, $x1, $y1);

        if ($beforename && file_exists($newpath)) {
            $newpaththumbs = ($annonce ? MY_ANNONCES_THUMBS : MY_THUMBS) . DS . $newname;
            @unlink($newpaththumbs);
            @unlink($newpath);
        }

        $maxh_resize = $annonce ? MAX_WIDTH_ANNONCE : MAX_HEIGHT_LOGO;

        if ($image->getWidth() > $maxh_resize) {
            $image->resizeToWidth($maxh_resize);
        }

        $destroy = ($beforename) ? FALSE : TRUE;
        $size = $image->getWidth() . 'x' . $image->getHeight();
        $image->save(NULL, 100, $destroy);

        @rename($oldpath, $newpath);

        if ($beforename) {
            $newPath = ($annonce ? MY_ANNONCES_THUMBS : MY_THUMBS) . DS . $newname;
            $image->resizeToHeight(24);
            $image->save($newPath, 100);
        }

        $data = array(
            "name" => $newname,
            "size" => $size
        );

        echo json_encode($data);
    }

    private function makeThumbLogo($namePath, $annonce = FALSE) {
        $oldPath = ($annonce ? MY_ANNONCES : MY_LOGOS) . DS . $namePath;
        $newPath = ($annonce ? MY_ANNONCES_THUMBS : MY_THUMBS) . DS . $namePath;

        copy($oldPath, $newPath);

        require JLPATH_LIBRARIES . DS . 'Tools' . DS . 'JLImage.class.php';

        $image = new JLImage();
        $image->load($newPath);
        $image->resizeToHeight(24);
        $image->save(NULL, 100);
    }

    function addLogo() {
        if ($this->isSetNoEmpty($_POST['txttitre']) && $this->isSetNoEmpty($_POST['cboetat'])) {

            require JLPATH_MODELS . DS . "LogosModel.php";
            $logos = new LogosModel();

            $var_title = $_POST['txttitre'];
            $txt_description = $_POST['txtdescription'];
            $dat_creation = date("d/m/Y h:i:s");
            $var_link = $_POST['txtlien'];
            $var_src = $_POST['name'];
            $var_size = $_POST['size'];
            $int_id_state = $_POST['cboetat'];
            $id_int_partenaire = $_POST['cboplanchelogo'];
            $annonce = !!$_POST['annonce'];

            $int_order = 0;

            if (!empty($var_src)) {
                $this->makeThumbLogo($var_src, $annonce);
            } else {
                $var_src = 0;
            }

            if ($annonce) {
                $logos->updateStateDesactive(0, 0);
            } else {
                $int_order = $logos->countLogoByIdPart($id_int_partenaire);
                $int_order++;
            }

            $b = $logos->insert($var_title, $txt_description, $dat_creation, $var_link, $var_src, $var_size, $int_id_state, $id_int_partenaire, $int_order);

            echo ($b) ? MSG_SUCCESS : MSG_ERROR;
        }
    }

    function getLogo() {
        if ($this->isSetNoEmpty($_POST['id'])) {

            $id = $_POST['id'];

            require JLPATH_MODELS . DS . "LogosModel.php";
            $logo = new LogosModel();
            $logos = $logo->getLogo($id);

            if ($logos) {
                $logos = $logos[0];

                $data = array(
                    'txttitre' => $logos['var_title'],
                    'txtlien' => $logos['var_link'],
                    'txtdescription' => $logos['txt_description'],
                    'name' => $logos['var_src'],
                    'size' => $logos['var_size'],
                    'cboetat' => $logos['int_id_state'],
                    'cboplanchelogo' => $logos['id_int_partenaire']);
            }

            echo json_encode(($logos) ? $data : null );
        }
    }

    function editLogo() {
        $int_id_logo = $_POST['id'];
        $var_title = $_POST['txttitre'];
        $txt_description = $_POST['txtdescription'];
        $var_link = $_POST['txtlien'];
        $var_src = $_POST['name'];
        $var_size = $_POST['size'];
        $int_id_state = $_POST['cboetat'];
        $id_int_partenaire = $_POST['cboplanchelogo'];
        $annonce = !!$_POST['annonce'];

        require JLPATH_MODELS . DS . "LogosModel.php";
        $logos = new LogosModel();

        $image = $logos->getLogo($int_id_logo);
        $image = $image[0]['var_src'];

        if ($image != $var_src) {
            $this->makeThumbLogo($var_src, $annonce);
        }

//        $txt_description = addcslashes($txt_description);

        if ($annonce) {
            $logos->updateStateDesactive(0, 0);
        }

        $b = $logos->update($int_id_logo, $var_title, $txt_description, $var_link, $var_src, $var_size, $int_id_state, $id_int_partenaire);

        echo ($b) ? MSG_SUCCESS : MSG_SUCCESS;
    }

    function sortLogos() {
        if ($this->isSetNoEmpty($_POST['data'])) {
            $data = str_replace("\\", "", $_POST['data']);
            $data = json_decode($data, true);

            require JLPATH_MODELS . DS . "LogosModel.php";
            $logos = new LogosModel();

            foreach ($data as $value) {
                $logos->updateSort($value["id"], $value["order"]);
            }

            echo $_POST['data'];
        }
    }

    function removeLogo() {
        if ($this->isSetNoEmpty($_POST['ids'])) {
            $int_id_logo = $_POST['ids'];
            $int_id_logo = str_replace("\\", "", $int_id_logo);
            $int_id_logo = str_replace("\"", "", $int_id_logo);
            $ls = explode(",", $int_id_logo);
            $annonce = $_POST['annonce'];

            require JLPATH_MODELS . DS . "LogosModel.php";
            $logos = new LogosModel();

            foreach ($ls as $value) {
                if (trim($value) != "") {
                    $image = $logos->getLogo($value);
                    $image = $image[0]['var_src'];

                    $b = $logos->delete($value);

                    if ($b) {
                        $logoPath = ($annonce ? MY_ANNONCES : MY_LOGOS) . DS . $image;
                        $thumbPath = ($annonce ? MY_ANNONCES_THUMBS : MY_THUMBS) . DS . $image;

                        if (file_exists($logoPath))
                            @unlink($logoPath);
                        if (file_exists($thumbPath))
                            @unlink($thumbPath);
                    }
                }
            }

            echo ($b) ? MSG_DELETE_SUCCESS : MSG_ERROR;
        }
    }

}